package com.cat.dynamicPlanning;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/ones-and-zeroes/
 * @create 2025/9/1 20:59
 * @since JDK17
 */

public class Solution47 {
    public int findMaxForm(String[] strs, int m, int n) {
        int len = strs.length;
        int[][] nums = new int[len][2];
        int[][][] dp = new int[len + 1][m + 1][n + 1];
        for (int i = 0; i < len; i++) {
            int zero = 0;
            for (int j = 0; j < strs[i].length(); j++) {
                if (strs[i].charAt(j) == '0') {
                    zero++;
                }
            }
            nums[i][0] = zero;
            nums[i][1] = strs[i].length() - zero;
        }


        for (int i = 1; i <= len; i++) {
            for (int j = 0; j <= m; j++) {
                for (int k = 0; k <= n; k++) {
                    dp[i][j][k] = dp[i - 1][j][k];
                    if (j >= nums[i - 1][0] && k >= nums[i - 1][1]) {
                        dp[i][j][k] = Math.max(dp[i][j][k], dp[i - 1][j - nums[i - 1][0]][k - nums[i - 1][1]] + 1);
                    }
                }
            }
        }

        return dp[len][m][n];
    }
}
